<?php

class TeamController extends \BaseController {

	/**
	 * Controller constructor
	 */
	public function __construct() {
		$this->beforeFilter('auth', array('except' => array('getAPI', 'getUsersAPI', 'updateUsersAPI', 'storeAPI', 'updateAPI', 'destroyAPI')));
	}
	
	/**
	 * Display a listing of teams.
	 *
	 * @return Response
	 */
	public function index()
	{
		//
	}


	/**
	 * Show the form for creating a new team.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}


	/**
	 * Store a newly created team in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}


	/**
	 * Display the specified team.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}


	/**
	 * Show the form for editing the specified team.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}


	/**
	 * Update the specified team in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}


	/**
	 * Remove the specified team from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

	/*
	 * API Methods
	*/
	/**
	 * Display a listing of teams in JSON format.
	 *
	 * @return JSON Response
	 */
	public function getAPI($id = null)
	{
		if (is_null($id))
			return Response::json(Team::all());
		else
		{
			$team = Team::find($id);
			if(is_null($team)){
				return Response::json('Team not found', 404);
			} else {
				return Response::json($team);
			}
		}
	}
	
	/**
	 * Display a listing of users in the specified team in JSON format.
	 *
	 * @param teamId
	 * @return JSON Response
	 */
	public function getUsersAPI($teamId)
	{
		$team = Team::find($teamId);
		if(is_null($team)){
			return Response::json('Team not found', 404);
		} else {
			$team->matches;
			$team->users;
			return Response::json($team);
		}
	}
	
	/**
	 * Store a newly created team in storage from JSON posted data.
	 *
	 * @return JSON Response
	 */
	public function storeAPI()
	{
		$newTeam = Input::json();
	
		$team = new Team();
		$team->name = $newTeam->get('name');
		$team->shirtColor = $newTeam->get('shirtColor');
		$team->save();
		
		// store relation to Match
		$matchId = $newTeam->get('matchId');
		$match = Match::find($matchId);
		$teamIds = array($team->id);
		if(is_array($teamIds))
		{
			$match->teams()->sync($teamIds);
		}
		
		return Response::json(array(
				"Team" => $team,
				"Match" => $match
			));
	}
	
	/**
	 * Update the specified team in storage from JSON put data.
	 *
	 * @return JSON Response
	 */
	public function updateAPI()
	{
		$updateTeam = Input::json();
	
		$team = Team::find($updateTeam->get('id'));
		if(is_null($team)){
			return Response::json('Team not found', 404);
		}
		$team->name = $updateTeam->get('name');
		$team->shirtColor = $updateTeam->get('shirtColor');
		$team->save();
		
		return Response::json($team);
	}
	
	/**
	 * Update specified users into specified team by JSON posted data.
	 *
	 * @return JSON Response
	 */
	public function updateUsersAPI()
	{
		$data = Input::json();
		
		// store relation to User
		$teamId = $data->get('teamId');
		$team = Team::find($teamId);
		$userIds = $data->get('userIds');
		if(is_array($userIds))
		{
			$team->users()->sync($userIds);
		}
	
		$team->matches;
		$team->users;
		return Response::json($team);
	}
	
	/**
	 * Remove the specified team from storage by posted id.
	 *
	 * @return JSON Response
	 */
	public function destroyAPI($id)
	{
		$team = Team::find($id);
	
		if(is_null($team))
		{
			return Response::json('Team not found', 404);
		}
		$deletedTeam = $team;
		$team->delete();
		return Response::json($deletedTeam);
	}

}
